<?php
namespace app\exam\model;

use app\exam\model\CandidateModel;
class SubjectModel extends CommonModel
{
    /*
        获取考题信息
        params:
            token 用户token
            subject_index 获取考题的下标 默认从1开始
        return arrray [
            'subject_title' => '最帅的人都有谁', //考试题目
            'subject_type' => 1, //考题类型 1-单选题 2-多选题 3-主观题
            'subject_option' => '提拉|群主|家旭|沈家旭' //考题选择项
        ]
            boolean 考试信息不存在
    */
    public function getSubjectInfo($user_token, $subject_index){
        $CandidateModel = new CandidateModel();

        $candidate = $CandidateModel->getCandidateInfoByToken($user_token);
        $subjectInfo = $this->getSubjectInfoByExamId($candidate['fk_exam_id'], $subject_index);

        if($subjectInfo == false){
            DLOG('考题信息不存在 '.json_encode(array($Candidate,$subject_index)), 'run', 'exam');
            return false;
        }

        if($subjectInfo['subject_type'] == 3){
            //主观题直接返回信息
            return $subjectInfo;
        }

        $subjectInfo['subject_option'] = explode('|', $subjectInfo['subject_option']);

        return $subjectInfo;

    }

    /*
        获取考题信息
        params:
            exam_id 考试id
            subject_index 考题下标
        return arrray [
            'subject_title' => '最帅的人都有谁', //考试题目
            'subject_type' => 1, //考题类型 1-单选题 2-多选题 3-主观题
            'subject_option' => '提拉|群主|家旭|沈家旭' //考题选择项
        ]
            boolean 考试信息不存在
    */
    public function getSubjectInfoByExamId($exam_id = 0, $subject_index)
    {
        $dao = db();
        $subjectDao = $dao->table('subject');

        $limit = $subject_index - 1;
        $sql = 'select subject_title,subject_type,subject_option from subject where fk_exam_id = ? limit '.$limit.', 1';
        
        $params = array(
            $exam_id
        );
        $subject = $subjectDao
            ->query($sql, $params);
        return empty($subject) == false ? $subject[0] : false;
    }

    /*
        初始化用户考试信息
        params:
            user_token 用户token
            exam_cnt 考试题数量
        return:
            boolean
    */
    public function initExamSubject($user_token = '', $exam_cnt = 1)
    {
        $subject = createArr($exam_cnt);
        $this->setExamSubject($user_token, $subject);

        return $this->getExamSubject($user_token);
    }

    /*
        设置用户考试记录
        params:
            user_token 用户token
            subject 考试记录
        return:
            boolean
    */
    public function setExamSubject($user_token = '', $subject = array())
    {
        $cache_key = 'exam_subject_'.$user_token;

        cache($cache_key, json_encode($subject));
        return true;
    }

    /*
        获取用户考试记录
        params:
            user_token 用户token
        return:
            array 考试记录
            boolean false 数据出错
    */
    public function getExamSubject($user_token = '')
    {
        $cache_key = 'exam_subject_'.$user_token;
        $subject = cache($cache_key);

        return empty($subject) == false ? $subject : false;
    }
}
?>